//**************************************************************************************************
//文件名:		main.c
//作  者:		sea wang
//日  期：		2016.10.13
//**************************************************************************************************
#include  "STM32_CB_V1.h"
#include  "my_gpio.h"
#include  "mipi_rffe.h"
#include  "my_safty.h"
#include  "my_systick.h"
#include "my_led.h"
#include "my_key.h"
#include "my_eeprom.h"
#include "my_hdc1080.h"
#include "cmd_service.h"
#include "mshell.h"
#include "my_typedef.h"
#include "my_adc.h"

i2c_t g_i2c;
i2c_t g_epm_i2c;
epm_24cxx_t g_epm;

led_t g_led1;
led_t g_led2;
uart_t g_uart1;
uart_t g_uart2;
rs485_t g_rs485;


skey_t sk1;
skey_t sk2;
skey_t sk3;
dkey_t dk;
systick_t stick;
hdc1080_t hdc1080;
uint8_t arry[8] = {0};
uint8_t arry1[8] = {0};

msh_t g_msh;
adc_t g_adc;

int main(void)
{
 //   float temp, humi;
    uint32_t i;
    int len;

	/*open global interrupt*/
	irq_enable();

    /* Set the Vector Table base location at 0x08000000 */
//    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x6000);

    /*系统定时器初始化*/
    api_systick_init(&stick, 10, ms);

    /*串口初始化*/
    //api_uart_init(&g_uart1, USART1, AF0, 115200, 256, 0, 1);
    api_uart_init(&g_uart2, USART2, AF0, 115200, 256, 0, 1);
    //api_rs485_init(&g_rs485, UART5, AF0, 115200, 24, 0, 1, PD0);
    printf("start...\r\n");
#if 1
    /*单按键初始化*/
    api_skey_init(&sk1, PE6, 900, 100, 0, &stick);
    api_skey_init(&sk2, PE5, 900, 100, 0, &stick);
    /*双启动安健初始化*/
    api_dkey_init(&dk, PE5, PE6, 100, 500, 0, 0, &stick);

    /*io初始化*/
    gpio_init();

    printf("start...\r\n");

    if(api_msh_init(&g_msh, &g_uart2, console_buffer, sizeof(console_buffer)) != 0)
    {
    	printf("msh init failed\r\n");
    }
    else
    {
    	printf("msh init success\r\n");
    	printf(CFG_PROMPT);
    }
    for(i = 0; i < sizeof(arry1); i++)
    {
        arry1[i] = i;
    }

    /*led初始化*/
    api_led_init(&g_led1, PA11, 500, 1000, 0, &stick);
    api_led_init(&g_led2, PA12, 500, 500, 0, &stick);
		
    /*i2c初始化*/
    api_i2c_init(&g_i2c, PD8, PD9, 4, 15000);
    api_i2c_init(&g_epm_i2c, PB14, PB13, 4, 4);
    /*eeprom初始化*/
    api_epm_init(&g_epm, &g_epm_i2c, AT_24C256);

    delay_ms(1000);

    if(hdc1080_init(&hdc1080, &g_i2c, Temp_14_bit, Humid_14_bit) < 0)
    {
        printf("hdc1080 init failed\r\n");
    }
    else
    {
        printf("hdc1080 init success\r\n");
    }

    {
        memset(arry, 0, sizeof(arry));
        api_epm_read(&g_epm, 0, arry, sizeof(arry));
        printf("read0:");
        print_hex(arry, sizeof(arry));

        api_epm_write(&g_epm, 0, arry1, sizeof(arry1));

        memset(arry, 0, sizeof(arry));
        api_epm_read(&g_epm, 0, arry, sizeof(arry));
        printf("read1:");
        print_hex(arry, sizeof(arry));
    }

    printf("RCC_CLK_CORE  : %dhz\r\n", api_rcc_clock_get(RCC_CLK_CORE  ));
    printf("RCC_CLK_AHB   : %dhz\r\n", api_rcc_clock_get(RCC_CLK_AHB   ));
    printf("RCC_CLK_APB1  : %dhz\r\n", api_rcc_clock_get(RCC_CLK_APB1  ));
    printf("RCC_CLK_APB2  : %dhz\r\n", api_rcc_clock_get(RCC_CLK_APB2  ));
    printf("RCC_CLK_ADC   : %dhz\r\n", api_rcc_clock_get(RCC_CLK_ADC   ));
    printf("RCC_CLK_TIM1_8: %dhz\r\n", api_rcc_clock_get(RCC_CLK_TIM1_8));
    printf("RCC_CLK_TIM2_TO_7: %dhz\r\n", api_rcc_clock_get(RCC_CLK_TIM2_TO_7));
#endif
    api_adc_init(&g_adc, ADC1, PA0, ADC_IN_16);
    delay_ms(2000);
    while(1)
    {

    	printf("temp:%f\r\n", calc_temprate(api_adc_read_times(&g_adc, 100)));
    	delay_ms(500);
    	/*按键事件处理*/
    	if (1)
    	{
			if(is_skey_long(&sk1))
			{
				printf("sk1 long press happened\r\n");
			}
			if(is_skey_short(&sk1))
			{
				printf("sk1 short press happened\r\n");
			}

			if(is_skey_long(&sk2))
			{
				printf("sk2 long press happened\r\n");
			}
			if(is_skey_short(&sk2))
			{
				printf("sk2 short press happened\r\n");
			}

			if(is_dkey_double(&dk))
			{
				printf("double key pressed happened\r\n");
			}
    	}

        /*mshell 例子*/
    	api_msh_service(&g_msh);


    }

    /*命令接收执行例子*/
    while (1)
    {
//        hdc1080_start_test(&g_i2c, &temp, &humi);
//        printf("temp:%f, humi:%f\n", temp, humi);
        len = api_uart_recv(&g_uart1, arry, sizeof(arry) - 1, 20);
        if(len > 0)
        {
            printf("recv:%s\n", arry);
//            api_do_simple_cmd(arry, "\r\n");
            api_do_cmd((char*) arry);
            memset(arry, 0, sizeof(arry));
        }
    }
}


extern char *optarg;
extern int optind, optopt;
uint8_t tbuf[128] = {0};
int do_test (mcmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    int ret;
    int len;
    uint8_t addr;
    uint8_t data;
	prtf("test cmd!\r\n");
    while ((ret = getopt(argc, argv, "a:w:l:r:")) != -1)
    {
        prtf("optind: %d\n", optind);
        switch (ret)
        {
        case 'a':
        	addr = my_atoi(optarg);
        	prtf("addr = %d\r\n", addr);
            break;
        case 'w':
        	memset(tbuf, 0, sizeof(tbuf));
        	ret = hexstr_to_array(optarg, tbuf, sizeof(tbuf));
        	if(ret < 0)
        	{
        		break;
        	}
        	print_hex(tbuf, ret);
        	prtf("write to addr %d, ret = %d\r\n", addr, ret);
        	api_epm_write(&g_epm, addr, tbuf, ret);
            break;
        case 'r':
        	len = my_atoi(optarg);
        	memset(tbuf, 0, sizeof(tbuf));
        	api_epm_read(&g_epm, addr, tbuf, len);
        	prtf("read form addr %d, len = %d\r\n", addr, len);
        	print_hex(tbuf, len);
            break;
        case '?':
            prtf("Unknown option: %c\r\n", (char)optopt);
            break;
        }
    }

	return 0;
}

API_MSH_CMD(
    test,     12,     1,      do_test,
	"test    - display test result\r\n",
	"test    - display test eeprom\r\n"
);



//**************************************************************************************************
//函 数 名:		SysTick_Handler（）
//功    能：  	嘀嗒定时器的中断函数
//返 回 值：	无
//**************************************************************************************************
void SysTick_Handler(void)
{
    api_led_sched(&g_led1);
    api_led_sched(&g_led2);
    api_skey_sched(&sk1, &dk);
    api_skey_sched(&sk2, &dk);
    api_dkey_sched(&dk);
}
